layui.define(["jquery"],function (exports) {
	var $ = layui.$;
	Calendar = function(){
		var t = this;
		t.config = {
			elem: "",
			time:'',
			paddingBottom:10,
			marginBottom:5, // layui-calendar-day的下方内边距
			headHeight:60, // 
			pickMode: 'single',// 日历的日期选择模式;single - 单选模式（默认值）;range - 范围选择模式;
			hasDate: true, //是否显示日期
			hasSwitcher: true,
			incident:[],
			callback:'',
			hasfn:'' // 自定义事件
		}
	};
	Calendar.prototype.set = function (option) {
		var t = this;
		$.extend(true, t.config,option);
		return t;
	},
	Calendar.prototype.render = function(option){
		var t = this;
		var obj = this.set(option).config;
		var thisMonth;
		if(obj.time){
			obj.date = new Date(obj.time);
		}else{
			if(obj.incident.length > 0){
				if(obj.incident[0].type == 'range'){
					var time = obj.incident[0].time.splice(",");
					obj.date = new Date(time[0]);
				}else{
					obj.date = new Date(obj.incident[0].time);
				}
			}else{
				obj.date = new Date();
			}
		}
		obj.height = $(obj.elem).height();
		obj.year = obj.date.getFullYear();
		obj.month = obj.date.getMonth()+1;
		obj.day = obj.date.getDate();
		obj.week = obj.date.getDay();
		obj.weekstart = getWeekDay(obj.year,obj.month-1,1);
		obj.monthdays = getMonthDays(obj.year,obj.month);
		var date = '<div class="layui-calendar-date" lay-switcher="'+ obj.hasSwitcher +'"><i class="layui-calendar-prev"><</i><span>' + obj.year + '-' + obj.month + '</span><i class="layui-calendar-next">></i></div>';
		var week = '<ul class="layui-calendar-week"><li>日</li><li>一</li><li>二</li><li>三</li><li>四</li><li>五</li><li>六</li></ul>';
		var div = $('<div class="layui-calendar"></div>');
		var days = '<ul class="layui-calendar-day">';
		var lh = (obj.height-obj.headHeight-obj.paddingBottom)/Math.ceil((obj.weekstart+obj.monthdays)/7)-obj.marginBottom;
		for(var i=0;i<obj.weekstart;i++){
			days += '<li style="line-height:'+lh+'px"></li>';
		}
		for(var i=1;i<obj.monthdays+1;i++){
			days += '<li style="line-height:'+lh+'px" class="';
			if(obj.incident.length > 0){
				var incident = obj.incident;
				for(var x in incident){
					if(obj.incident[x].type == 'range'){
						var times = obj.incident[x].time.split(",");
						if(times.length == 2){
							if(getTmonth(times[0]) == obj.month && getTmonth(times[1]) == obj.month){
								if(getTdate(times[0])-1 < i && i < getTdate(times[1])+1){
									if(i == getTdate(times[0])){
										days += ' layui-calendar-range layui-calendar-rangestart" data-info="{time:'+obj.incident[x].time+',info:'+obj.incident[x].info+'}';
									}else if(i == getTdate(times[1])){
										days += ' layui-calendar-range layui-calendar-rangeend" data-info="{time:'+obj.incident[x].time+',info:'+obj.incident[x].info+'}';
									}else{
										days += ' layui-calendar-range" data-info="{time:'+obj.incident[x].time+',info:'+obj.incident[x].info+'}';
									}
								}
							}
							if(getTmonth(times[0]) < obj.month && getTmonth(times[1]) == obj.month){
								if(i < getTdate(times[1])+1){
									if(i == getTdate(times[1])){
										days += ' layui-calendar-range layui-calendar-rangeend" data-info="{time:'+obj.incident[x].time+',info:'+obj.incident[x].info+'}';
									}else{
										days += ' layui-calendar-range" data-info="{time:'+obj.incident[x].time+',info:'+obj.incident[x].info+'}';
									}
								}
							}
							if(getTmonth(times[0]) == obj.month && getTmonth(times[1]) > obj.month){
								if(i > getTdate(times[0])-1){
									if(i == getTdate(times[0])){
										days += ' layui-calendar-range layui-calendar-rangestart" data-info="{time:'+obj.incident[x].time+',info:'+obj.incident[x].info+'}';
									}else{
										days += ' layui-calendar-range" data-info="{time:'+obj.incident[x].time+',info:'+obj.incident[x].info+'}';
									}
								}
							}
						}else{
							console.error(obj.incident[x].time+"日期范围不对");
						}
					}else{
						if(getTmonth(obj.incident[x].time) == obj.month){
							if(getTdate(obj.incident[x].time) == i){ 
								days += ' layui-calendar-'+obj.incident[x].type+'" data-tips="'+obj.incident[x].info;
							}
						}
					}
				}
			}
			days += '">'+i+'</li>';
		}
		days += '</ul>';
		if(obj.hasDate){
			div.html(date + week + days);
		}else{
			div.html(week + days);
		}
		$(obj.elem).html(div);
		if(obj.hasfn){
			obj.hasfn();
		}
		$(obj.elem).off("click",'.layui-calendar-prev');
		$(obj.elem).on("click",'.layui-calendar-prev',function(){
			var time = $(this).next().text();
			var year = getYear(time);
			var month = getTmonth(time)-1;
			if(month == 0){
				year -= 1;
				month = 12;
			}
			obj.time = year + '-' + month;
			t.render(obj);
		})
		$(obj.elem).off("click",'.layui-calendar-next');
		$(obj.elem).on("click",'.layui-calendar-next',function(){
			var time = $(this).prev().text();
			var year = getYear(time);
			var month = getTmonth(time)+1;
			if(month == 13){
				year += 1;
				month = 1;
			}
			obj.time = year + '-' + month;
			t.render(obj);
		})
		/*$(obj.elem).off("click",'.layui-calendar-day li');
		$(obj.elem).on("click",'.layui-calendar-day li',function(){
			var day = $(this).text();
			if(day.length>0){
				switch(obj.pickMode){
					case 'range';
					break;
					default:
						
					break;
				}
			}
		})*/
	}
	var getMonthDays = function(year,month){
      var date=new Date(year,month,0);
      return date.getDate();
    },
	getYear = function(time){
		var d = new Date(time);
		return d.getFullYear();
    },
	getTmonth = function(time){
		var d = new Date(time);
		return d.getMonth()+1;
    },
	getTdate = function(time){
		var d = new Date(time);
		return d.getDate();
    },
    getWeekDay = function(year,month,day){
      var date=new Date(year,month,day);
      return date.getDay();
    };
	var calendar = new Calendar();
	exports('calendar',calendar);
})